package com.hancher.gamelife2.birthday;

import android.view.LayoutInflater;
import android.view.ViewGroup;

import com.bumptech.glide.Glide;
import com.hancher.common.androidutil.LogUtil;
import com.hancher.common.javautil.EmptyUtil;
import com.hancher.common.rv.BaseVBAdapter;
import com.hancher.gamelife2.R;
import com.hancher.gamelife2.databinding.RecycleritemBirthday2Binding;
import com.nlf.calendar.Lunar;
import com.nlf.calendar.Solar;

import java.util.Date;

/**
 * 作者 : Hancher ytu_shaoweijie@163.com <br/>
 * 时间 : 2023/5/21 17:37 <br/>
 * 描述 :
 */
public class BirthdayListAdapter extends BaseVBAdapter<RecycleritemBirthday2Binding, BirthdayPO> {


    @Override
    protected RecycleritemBirthday2Binding createViewBinding(LayoutInflater from, ViewGroup viewGroup) {
        return RecycleritemBirthday2Binding.inflate(from, viewGroup, false);
    }

    @Override
    protected void convertItem(RecycleritemBirthday2Binding binding, int position, BirthdayPO item) {
        binding.itemName.setText(item.getName());
        if (EmptyUtil.isNotEmpty(item.getImgUrl())) {
            Glide.with(binding.imgHeader).load(item.getImgUrl()).into(binding.imgHeader);
        } else {
            Glide.with(binding.imgHeader).load(R.drawable.people_zhangsan).into(binding.imgHeader);
        }
        if (item.getIsLunarType()) {
            Lunar current = Lunar.fromDate(new Date());
            Lunar birthday = new Lunar(item.getLunarY(), Math.abs(item.getLunarM()), item.getLunarD());
            Lunar currentYearBirthday = null;
            try {
                currentYearBirthday = new Lunar(current.getYear(), Math.abs(item.getLunarM()), item.getLunarD());
            } catch (Exception e) {
                LogUtil.w("阴历转换失败", e);
            }
            if (currentYearBirthday == null || currentYearBirthday.getSolar().subtract(current.getSolar()) < 0) {
                currentYearBirthday = new Lunar(current.getYear() + 1, Math.abs(item.getLunarM()), item.getLunarD());
            }
            binding.itemAge.setText((current.getYear() - birthday.getYear()) + "岁生日");
//            Glide.with(binding.imgSolarLunar).load(R.drawable.item_lunar).into(binding.imgSolarLunar);
            binding.imgSolarLunar.setImageResource(R.drawable.item_lunar);
            binding.itemCoutdown.setText(currentYearBirthday.getSolar().subtract(current.getSolar()) + "");
            binding.txtDate.setText(birthday.toString());
            binding.txtShengxiao.setText(String.format(" / %s / %s", birthday.getYearShengXiao(), birthday.getSolar().getXingZuo()));
        } else {
            Solar current = Solar.fromDate(new Date());
            Solar birthday = new Solar(item.getSolarY(), item.getSolarM(), item.getSolarD());
            Solar currentYearBirthday = null;
            try {
                currentYearBirthday = new Solar(current.getYear(), item.getSolarM(), item.getSolarD());
            } catch (Exception e) {
                LogUtil.w("阳历转换失败", e);
            }
            if (currentYearBirthday.subtract(current) < 0) {
                currentYearBirthday = new Solar(current.getYear() + 1, item.getSolarM(), item.getSolarD());
            }
            binding.itemAge.setText((current.getYear() - birthday.getYear()) + "岁生日");
//            Glide.with(binding.imgSolarLunar).load(R.drawable.item_solar).into(binding.imgSolarLunar);
            binding.imgSolarLunar.setImageResource(R.drawable.item_solar);
            binding.itemCoutdown.setText(currentYearBirthday.subtract(current) + "");
            binding.txtDate.setText(birthday.toString());
            binding.txtShengxiao.setText(String.format(" / %s / %s", birthday.getLunar().getYearShengXiao(), birthday.getXingZuo()));
        }
    }

}
